define(function () {
    'use strict';

    function ctrl($scope, $ionicLoading, $state, $q, CommonService, WebService, $ionicPopup, TdxLocalFuncService) {

        var par = $scope.param = {};

        $scope.$on('$ionicView.beforeEnter', function () {
            init();
        });

        $scope.doRefresh = function () {
            init().finally(function () {
                $scope.$broadcast('scroll.refreshComplete');
            });
        };

        function init() {
            $scope.q_bank = new Array();
            $scope.d_bank = null;
            par.is_hunme = false;
            //理财体验 不显示存管相关
            if (sessionStorage["tf_hunme_from"] == "2" || CommonService.isBuyExperience()) {
                par.is_hunme = true;
            }
            return $q.all([qryDeposit()]);
        }

        //查询用户存管银行
        function qryDeposit() {
            var q = WebService.getUserDepositBankCards();
            q.then(
                function (data) {
                    if (data.length > 0) {
                        $scope.d_bank = data[0];
                        //待审核、待激活状态 预指定银行不显示银行卡尾号
                        if ("1,3".indexOf($scope.d_bank.bkaccount_regflag) > -1 && "2" == $scope.d_bank.contract_way) {
                            $scope.d_bank.bank_account_short = '';
                            $scope.d_bank.un_card = 'unCard';
                        } else {
                            var bank_account = $scope.d_bank.bank_account;
                            $scope.d_bank.bank_account_short = '<span>尾号(' + bank_account.substr(bank_account.length - 4) + ')</span>';
                            if ("农业银行" == $scope.d_bank.bank_name || "建设银行" == $scope.d_bank.bank_name) {
                                $scope.d_bank.showIcon = true;
                            }
                            $scope.d_bank.un_card = 'unCard';
                        }
                        if ("1,3".indexOf($scope.d_bank.bkaccount_regflag) > -1) {
                            $scope.d_bank.deposit_class = 'unfinished';
                        } else {
                            $scope.d_bank.deposit_class = '';
                        }
                    }
                },
                function (result) {
                    CommonService.showAlert({message: CommonService.getErrorInfo(result)});
                }
            );
            return q;
        }

        $scope.showConfigDetail = function () {
            $ionicPopup.show({
                cssClass: "kDialog",
                template: "由于银行系统原因，此处展示的4位数字为银行分配的账号尾号，而不是实际银行卡的末4位数字。",
                buttons: [
                    {text: '我知道了'}
                ]
            });
        };

        $scope.unRegister = function () {
            TdxLocalFuncService.tdxTradeLocked().then(function(data){
                if(data.text=='1' || data.text==1){
                    return;
                }else{
                    WebService.checkCancelTime().then(
                        function (data) {
                            if (data.yhyw_time_flag != 1) {
                                $ionicPopup.show({
                                    cssClass: "kDialog",
                                    template: "当前为非交易时间，请在交易日09:00-16:00内办理撤销。",
                                    buttons: [
                                        {text: '确定'}
                                    ]
                                });
                            } else {
                                CommonService.showConfirm(
                                    {
                                        message: "请确认是否要办理预指定银行卡撤销？点击“确认”表示办理并同意《三方存管银行变更申请表》。"
                                    }, function () {
                                    }, function () {
                                        $ionicLoading.show();
                                        WebService.cancelBankAccount2($scope.d_bank.bank_no).then(
                                            function (data) {
                                                CommonService.showAlert({message: "撤销成功!"});
                                                $state.go('tab.account-manager');
                                                $ionicLoading.hide();
                                            },
                                            function (result) {
                                                CommonService.showConfig({message: "很抱歉，您的预指定银行卡撤销失败！如有疑问，请拨打客服热线：400-800-5000"});
                                                $ionicLoading.hide();
                                            }
                                        );

                                    }
                                )
                            }
                        },
                        function (result) {
                            CommonService.showConfig({message: CommonService.getErrorInfo(result)});
                        }
                    );
                }
            })
        };

        $scope.showActivateMsg = function () {
            CommonService.showConfig({okText: "关闭", message: $scope.d_bank.brief});
        };
    }

    ctrl.$inject = ['$scope', '$ionicLoading', '$state', '$q', 'CommonService', 'WebService', '$ionicPopup','TdxLocalFuncService'];
    return ctrl;
});